// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 导入mint-ui
import mint from 'mint-ui'
//导入mint-ui样式
import 'mint-ui/lib/style.css'
//导入时间插件
import moment from 'moment'
//定义全局过滤器   moment这个插件不传参数可以获取当前时间，之后需要学习一下
Vue.filter('dateFormat',function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
  return moment(dataStr).format(pattern)
})
//导入mint-ui
import Mui from  'vue-awesome-mui'
//导入mui样式
import 'vue-awesome-mui/mui/dist/css/mui.css'


//导入 vue-resource
import VueResource from 'vue-resource'
//设置请求根路径
// Vue.http.options.root = 'http://localhost:3000'
//导入 vuex
import Vuex from 'vuex'
import VuePreview from "vue-preview";
Vue.use(VuePreview);

Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: { top: 0, bottom: 0 },
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})



import 'es6-promise/auto'
//安装
Vue.use(Vuex)

var car=JSON.parse(localStorage.getItem('car')||'[]')
var store=new Vuex.Store({
  state:{//this.$store.state.***
    car:car
  },
  mutations:{//this.$store.commint.('方法名称'，'按需传递唯一的参数')
    addToCar(state,goodsinfo){
      var flag = false
      state.car.some(item=>{
        if(item.id == goodsinfo.id){
          item.count+=parseInt(goodsinfo.count)
          flag = true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsinfo)
      }

      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateGoodsInfo(state,goodsinfo){
      state.car.some(item=>{
        if(item.id==goodsinfo.id){
          item.count=parseInt(goodsinfo.count)
          return true
        }
      })
    },
    removeFormCar(state,id){
      state.car.some((item,i)=>{
        if(item.id == id){
          state.car.splice(i,1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters:{//this.$store.getters.***
    getAllCount(state){
      var c=0
      state.car.forEach(item=>{
        c+=item.count
      })
      return c
    },
    getGoodsCount(state){
      var o = []
      state.car.forEach(item=>{
        o[item.id]=item.count
      })
      return o
    }

  }
})
//安装mint-ui
Vue.use(mint)
//安装MUI
Vue.use(Mui)
//安装 vue-resource
Vue.use(VueResource)


Vue.config.productionTip = false

Vue.http.options.emulateJSON =true
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  watch:{
    
  }
})
// Vue.use(preview, {
//   mainClass: 'pswp--minimal--dark',
//   barsSize: { top: 0, bottom: 0 },
//   captionEl: false,
//   fullscreenEl: false,
//   shareEl: false,
//   bgOpacity: 0.85,
//   tapToClose: true,
//   tapToToggleControls: false
// })